// Dart imports:
import 'dart:io';

// Package imports:
import 'package:get/get.dart';
import 'package:palmf_hid/common/utils/utils.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';

// Project imports:

class DatabaseBackupUtil {
  //数据库备份路径
  static Future<String> get dbBackPath async {
    String savePath = "";

    ///判断手机系统型号
    if (Platform.isAndroid) {
      savePath = "/sdcard/Download/" + "hidDbSave.db";
    } else if (Platform.isIOS) {
      Directory appDocDir = await getLibraryDirectory();
      String dir = appDocDir.path;
      savePath = dir + "/" + "hidDbSave.db";
    }
    return savePath;
  }

  //数据库路径
  static Future<String> get dbPath async {
    //通过路径提供器获取目录以存放数据库文件
    final path = (Platform.isMacOS || Platform.isIOS)
        ? await getApplicationDocumentsDirectory()
        : await getApplicationSupportDirectory();

    //这是数据库文件。
    final dbFile = p.join(path.path, 'databases', 'palmF.db');
    return dbFile;
  }

  //备份数据库
  static void backDatabase() async {
    final backPath = await dbBackPath;
    final db = await dbPath;
    File(db).copy(backPath).then((value) {
      if (value.path == backPath) {
        MyLogUtil().i(value);
        MyToastUtil.show("backupOk".tr);
      } else {
        MyToastUtil.show("backupFailed".tr);
      }
    });
  }

  //恢复数据库
  static Future<bool> restoreDatabase() async {
    final backPath = await dbBackPath;
    final db = await dbPath;
    var value = await File(backPath).copy(db);
    MyLogUtil().i(value);
    if (value.path == db) {
      MyToastUtil.show("restoreOk".tr);
      return true;
    } else {
      MyToastUtil.show("restoreFailed".tr);
      return false;
    }
  }
}
